﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Contract.Customer.Response.PortfolioManagement;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.PortfolioManagement
{
    public class RetrieveAccountPlanningProductHoldingServiceCommand: ProviderServiceCommand<IProviderFactory, RetrieveDataAccountPlanningByCifRequest, RetrieveDataAccountPlanningByCifResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveAccountPlanningProductHoldingServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveDataAccountPlanningByCifRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
        }

        protected override bool OnExecute(RetrieveDataAccountPlanningByCifResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<DataAccountPlanningRecord> records = new List<DataAccountPlanningRecord>();
            List<AccountPlanningProductHolding> listCustomer = new List<AccountPlanningProductHolding>();
            listCustomer = Providers.Portfolios.RetrieveAccountPlanningProductHoldingByCif(Cif);
            if (listCustomer != null && listCustomer.Count > 0)
            {
                records = listCustomer.Select(x => new DataAccountPlanningRecord
                {
                    Cif = Cif,
                    GroupCri = x.GroupCri,
                    LastYear = x.LastYear,
                    Year = x.Year,
                    Month1 = x.Month1,
                    Month2 = x.Month2,
                    Month3 = x.Month3,
                    Month4 = x.Month4,
                    Month5 = x.Month5,
                    Month6 = x.Month6,
                    Month7 = x.Month7,
                    Month8 = x.Month8,
                    Month9 = x.Month9,
                    Month10 = x.Month10,
                    Month11 = x.Month11,
                    Month12 = x.Month12,
                    DataType = x.DataType,
                    Name = x.Name.Trim(),
                    ChangeDate = x.ChangeDate,
                    CreatedDate = x.CreatedDate,
                    GroupRow = x.GroupRow
                }).ToList();
            }
            response.Records = records;
            return isSuccess;
        }
    }
}
